package com.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.web.domain.Category;
import com.web.domain.PageBean;
import com.web.domain.Product;
import com.web.service.AdProductService;
import com.web.service.BaseService;
import com.web.service.CategoryService;
import com.web.utils.BeanFactory;

/**
 * Servlet implementation class AdProductServlet
 */
@WebServlet("/adproduct") 
public class AdProductServlet extends BaseService {
	private static final long serialVersionUID = 1L;
	private AdProductService aps = (AdProductService) BeanFactory.getBean("AdProductService");
	/*
	 * 查询所有商品
	 * @see com.web.service.BaseService#index(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public String findProduct(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			List<Product> p = aps.findProduct();
			request.setAttribute("p", p);
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "/admin/product/adproduct.jsp";
	}
	
	/*
	 * 分页查找
	 */
	public  String findByAdPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			int pageNumber = 1;
			pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
			
			int pageSize = 9;
			AdProductService aps = (AdProductService) BeanFactory.getBean("AdProductService");
			PageBean<Product> adbean = aps.findByAdPage(pageNumber,pageSize);
			request.setAttribute("adbean", adbean);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "/admin/product/adproduct.jsp";
	}
	
	/*
	 * 根据id删除商品
	 */
	public String delProduct(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			String pid = request.getParameter("pid");
			aps.delProduct(pid);
			String json ="{\"msg\":\"200\"}";
			response.getWriter().println(json);
		} catch (Exception e) {
			String json ="{\"msg\":\"no\"}";
			response.getWriter().println(json);
		}
		return null;
	}
	
	public String show_Product(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			CategoryService cs = (CategoryService) BeanFactory.getBean("CategoryService");
			List<Category> cg = cs.findCategory();
			request.setAttribute("cg", cg);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/admin/product/add_product.jsp";
	}
	
}















